package cn.tiancaifan.micro.controller.stu;


import cn.tiancaifan.micro.entity.*;
import cn.tiancaifan.micro.service.DepartmentService;
import cn.tiancaifan.micro.service.StudentService;
import cn.tiancaifan.micro.utils.AliyunOSSUtil;
import cn.tiancaifan.micro.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/stu/basic")
public class StuBasicController {

    @Autowired
    StudentService studentService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    ConstantProperties constantProperties;

    @Autowired
    AliyunOSSUtil aliyunOSSUtil;


    @GetMapping("/")
    public RespPageBean getStudentByPage(Student student, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size){
        return studentService.getStudentByPage(student,page,size);
    }

    @DeleteMapping("/{id}")
    public SystemResults deleteStudentById(@PathVariable("id") Integer id){
        if (studentService.deleteStudentById(id) == 1) {
            return SystemResults.ok("删除成功!");
        }
        return SystemResults.error("删除失败!");
    }

    @PostMapping("/")
    public SystemResults addStu(@RequestBody Student student){
        return studentService.addStu(student);
    }

    @PutMapping("/")
    public SystemResults updateStudentById(@RequestBody Student student) {
        return studentService.updateStudentById(student);
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData() {
        List<Student> list = (List<Student>) studentService.getStudentByPage(null, null, null).getData();
        return POIUtils.student2Excel(list);
    }

    @PostMapping("/import")
    public SystemResults importData(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        InputStream input = file.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = input.read(buffer)) > -1 ) {
            baos.write(buffer, 0, len);
        }
        baos.flush();
        InputStream stream1 = new ByteArrayInputStream(baos.toByteArray());
        InputStream stream2 = new ByteArrayInputStream(baos.toByteArray());
        String upload = aliyunOSSUtil.upload(constantProperties.getExcel()+fileName+new Date().getTime()+".xls", stream1);
        List<Student> students = POIUtils.excel2Student(stream2, ((List<Nation>) studentService.getNations().getData())
                , ((List<Politicsstatus>) studentService.getPoliticsstatus().getData())
                , ((List<Department>) studentService.getDeps().getData())
                , studentService.getPositions()
                , ((List<Job>) studentService.getJobs().getData()));
        return studentService.addStus(students);
    }

    @GetMapping("/maxWorkID")
    public SystemResults maxWorkID(){
        return studentService.maxWorkID();
    }

    @GetMapping("/nations")
    public SystemResults getNations(){
        return studentService.getNations();
    }

    @GetMapping("/job")
    public SystemResults getJobs(){
        return studentService.getJobs();
    }

    @GetMapping("/politicsstatus")
    public SystemResults getPoliticsstatus(){
        return studentService.getPoliticsstatus();
    }

    @GetMapping("/deps")
    public SystemResults getAllDepartment(){
        return SystemResults.ok(null,departmentService.getAllDepartment());
    }

    @GetMapping("/positions")
    public SystemResults getPositions(){
        return SystemResults.ok(null,studentService.getPositions());
    }
}
